<?php
class Admin_Model extends CI_Model {
  function __construct() {
    $this->load->database ();
  }
  function login($username, $password) {
    try {
      $this->db->select ( '*' )
        ->where ( 'username', $username )
        ->where ( 'password', md5($password ));
      $result = $this->db->get('admin')->row();
      if (count($result)) {
        return $result->id;
      } else {
        return null;
      }
    } catch ( Exception $ex ) {
      return null;
    }
  }
  function count_all() {
    return $this->db->count_all ( 'admin' );
  }
  function get_by_id($user_id) {
    try {
      $this->db->select ( '*' )
      ->from ( 'admin' )
      ->where ( 'id', $user_id );
      $query = $this->db->get ();
      return $query->result ();
    } catch ( Exception $ex ) {
      return false;
    }
  }
  function change_password_admin($admin_id, $data) {
    try {
      $data_update = array ();
      // set pass
      $old_data = $this->get_by_id ( $admin_id );
      foreach ( $old_data [0] as $k => $v ) {
        $data_update [$k] = $v;
      }
      $data_update ['password'] = md5($data['password']);
      $this->db->where ( 'id', $admin_id );
      $this->db->update ( 'admin', $data_update );
      return true;
    } catch ( Exception $ex ) {
      return false;
    }
  }
}
?>